import QtQuick 2.12
import QtQuick.Window 2.1
import FileOP 1.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import QtQuick.Dialogs 1.3
import QtCharts 2.1
import QtQml 2.12
Window {
    id:root
    width: Screen.desktopAvailableWidth
    height: Screen.desktopAvailableHeight
    visible: true
    title: qsTr("气象数据分析")
    property int ser_start: 0
    property int ser_end: 0
    property int ser_count: 0

    function showDat(num)
    {
        var myjson = myList.get(num)
        var tmm = fileIO.getTime(myjson.timestamp)
//        var mydate = new Date(myjson.timestamp)
//        mydate.toLocaleString(Qt.locale("de_DE"), Locale.ShortFormat)
//        time_lable.text = mydate.toLocaleString(Qt.locale("de_DE"), "yyyy-MM-dd HH:mm:ss")//mydate.toDateString()
        time_lable.text = tmm;
        att_lable.text = myjson.gps_att.toString()
        lon_lable.text = myjson.gps_lon.toString()
        lat_lable.text = myjson.gps_alt.toString()
        speed_lable.text = myjson.wind_speed.toString()
        yaw_lable.text = myjson.wind_yaw.toString()
        temp_lable.text = myjson.temperature.toString()
        humidity_lable.text = myjson.humidity.toString()
    }

    function readList(name)
    {
        var cnt = 0;
        while(1)
        {
            var line = fileIO.readLine(name,cnt);
            if(line === "")
            {
                break
            }
            cnt = cnt + line.length + 1;
            var myjson = JSON.parse(line);
            myList.append(myjson)
        }
    }
    function toolFun(str)
    {
        if(str === "open")
        {
            fdopen.open()
        }else if(str === "in")
        {
            chart1.zoomIn()
            chart2.zoomIn()
        }
        else if(str === "out")
        {
            chart1.zoomOut()
            chart2.zoomOut()
        }else{
            chart1.zoomReset()
            chart2.zoomReset()
        }
    }

    ListModel{
        id:myList
    }
    FileOP{
        id:fileIO
    }
    function updateDat()
    {
        for(var i = 0 ; i < myList.count ; i++)
        {
            chart1.updateChart(myList.get(i).wind_speed,myList.get(i).gps_alt);
            chart2.updateChart(myList.get(i).temperature,myList.get(i).humidity);
        }
    }

    FileDialog{
        id:fdopen
        selectExisting:true
        onAccepted: {
            var libStr = fdopen.fileUrl.toString();
            console.info(libStr);
            var libPath = libStr.substring(7,libStr.length);
            console.info(libPath)
            readList(libPath)
            chart1.resetChart()
            ser_start = 0
            ser_count = 0
            ser_end = myList.count
            helloTimer.start()
            //            updateDat()
        }
    }

    Rectangle{
        id:shows1
        anchors.top: parent.top
        anchors.topMargin: 4
        height: 4 * parent.height / 5 - 120
//        anchors.bottom: parent.bottom
//        anchors.bottomMargin: 4

        anchors.left: parent.left
        anchors.leftMargin: 4

        anchors.right: parent.right
        anchors.rightMargin: 4

        ShowChart{
            id:chart1
            width: parent.width
            height: 2 * root.height / 5 - 60
            anchors.top: parent.top
            anchors.topMargin: 4

        }
        ShowChart3{
            id:chart2
            width: parent.width
            height: 2 * root.height / 5 - 60
            anchors.top: chart1.bottom
            anchors.topMargin: 4
        }
    }
    Timer{
        id:helloTimer
        interval: 10; //定时周期
        repeat:true;    //
        triggeredOnStart: true;
        onTriggered: {
            if(ser_count < ser_end)
            {
                chart1.updateChart(myList.get(ser_count).wind_speed,myList.get(ser_count).gps_alt);
                chart2.updateChart(myList.get(ser_count).temperature,myList.get(ser_count).humidity);
                ser_count++
            }
            else
            {
                helloTimer.stop()
            }
        }
    }
    ToolBar{
        id:tb
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 70
    }
    Rectangle{
//        width: parent.width
        height: 100
        anchors.top: shows1.bottom
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 200
        anchors.right: parent.right
        anchors.rightMargin: 200
        antialiasing:true
        implicitWidth:  parent.width / 8
        GridLayout{
            rows: 2
            columns: 8
//            columnSpacing: 4
//            rowSpacing: 4
            anchors.fill: parent
            z:10

            Label{
                text: "时间"

            }
            Label{
                id:time_lable
                x:parent.width / 8
            }
            Label{
                text: "海拔"
                x:parent.width / 4
            }
            Label{
                id:att_lable
                x:3 * parent.width / 8
            }
            Label{
                text: "经度"
                x:parent.width / 2
            }
            Label{
                id:lon_lable
                x:5 * parent.width / 8
            }
            Label{
                text: "维度"
                x:3 * parent.width / 4

            }
            Label{
                id:lat_lable
                x:7 * parent.width / 8
            }

            Label{
                text: "风速"

            }
            Label{
                id:speed_lable
            }

            Label{
                text: "风向"

            }
            Label{
                id:yaw_lable

            }

            Label{
                text: "温度"

            }
            Label{
                id:temp_lable

            }

            Label{
                text: "湿度"

            }
            Label{
                id:humidity_lable
            }
        }
    }
}
